# Contributor: Sergey Lukin <sergej.lukin@gmail.com>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=icu
pkgver=71.1
pkgrel=2
pkgdesc="International Components for Unicode library"
url="https://icu.unicode.org/"
arch="all"
license="MIT ICU Unicode-TOU"
depends_dev="$pkgname=$pkgver-r$pkgrel"
makedepends="python3 py3-yaml"
checkdepends="diffutils"
install="$pkgname-data-en.post-install"
subpackages="
	$pkgname-static
	$pkgname-dev
	$pkgname-doc
	$pkgname-libs
	$pkgname-data-en:_data_en:noarch
	$pkgname-data-full:_data_full:noarch
	"
source="https://github.com/unicode-org/icu/releases/download/release-${pkgver//./-}/icu4c-${pkgver//./_}-src.tgz
	https://github.com/unicode-org/icu/releases/download/release-${pkgver//./-}/icu4c-${pkgver//./_}-data.zip
	https://github.com/unicode-org/icu/releases/download/release-${pkgver//./-}/icu4c-${pkgver//./_}-data-bin-b.zip
	https://github.com/unicode-org/icu/releases/download/release-${pkgver//./-}/icu4c-${pkgver//./_}-data-bin-l.zip
	001-fix-heap-buffer-overflow.patch
	skip-flawed-tests.patch
	data-filter-en.yml
	"
builddir="$srcdir/icu/source"

# secfixes:
#   66.1-r0:
#     - CVE-2020-21913
#   65.1-r1:
#     - CVE-2020-10531
#   57.1-r1:
#     - CVE-2016-6293
#   58.1-r1:
#     - CVE-2016-7415
#   58.2-r2:
#     - CVE-2017-7867
#     - CVE-2017-7868

case "$CARCH" in
	s390x) _icudtfile=icudt${pkgver%%.*}b.dat;;
	*) _icudtfile=icudt${pkgver%%.*}l.dat;;
esac

prepare() {
	default_prepare

	rm -rf data
	mv "$srcdir"/data .
}

build() {
	_yaml2json "$srcdir"/data-filter-en.yml > data-filter-en.json
	export ICU_DATA_FILTER_FILE="./data-filter-en.json"

	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--with-data-packaging=archive \
		--disable-samples \
		--enable-static \
		--mandir=/usr/share/man
	make
}

check() {
	# Tests need full data.
	mv data/out/$_icudtfile data/out/$_icudtfile.en
	cp -a "$srcdir"/$_icudtfile data/out/

	CINTLTST_OPTS='-w' make check

	mv data/out/$_icudtfile.en data/out/$_icudtfile
}

package() {
	make -j1 DESTDIR="$pkgdir" install

	chmod +x "$pkgdir"/usr/bin/icu-config

	install -Dm644 "$srcdir"/icu/license.html -t "$pkgdir"/usr/share/licenses/icu/
}

dev() {
	default_dev

	amove usr/lib/icu
	amove usr/lib/libicutest.so.*
	amove usr/share/icu/*/LICENSE
	amove usr/share/icu/*/config
	amove usr/share/icu/*/install-sh
	amove usr/share/icu/*/mkinstalldirs
}

libs() {
	pkgdesc="$pkgdesc (libraries)"
	depends="$pkgname-data=$pkgver-r$pkgrel"
	replaces="icu"

	# Data stub required by the Common and I18n libraries.
	amove usr/lib/libicudata.so.[0-9]*
	# A library that contains many locale based internationalization (i18n) functions.
	amove usr/lib/libicui18n.so.[0-9]*
	# An optional library that provides a stdio like API with Unicode support.
	amove usr/lib/libicuio.so.[0-9]*
	# Base library required by all other ICU libraries.
	amove usr/lib/libicuuc.so.[0-9]*

	# An internal library that contains internal APIs that are only used by ICU’s tools.
	# Keep in the base package.
	#amove usr/lib/libicutu.so.[0-9]*
}

_data_en() {
	pkgdesc="Stripped down ICU data with only en_US/GB locale and no legacy charset converters"
	provides="$pkgname-data=$pkgver-r$pkgrel"
	provider_priority=100  # highest (other provider is icu-data-full)
	replaces="$pkgname-data<71.1-r1"

	amove usr/share/icu/$pkgver/$_icudtfile
}

_data_full() {
	pkgdesc="Full ICU data"
	provides="$pkgname-data=$pkgver-r$pkgrel"
	provider_priority=10  # lowest (other provider is icu-data-en)
	replaces="$pkgname-data<71.1-r1"

	install -D -m644 "$srcdir"/$_icudtfile -t "$subpkgdir"/usr/share/icu/$pkgver/
}

_yaml2json() {
	python3 -c 'import sys, yaml, json; json.dump(yaml.safe_load(sys.stdin), sys.stdout)' <"$1"
}

sha512sums="
1fd2a20aef48369d1f06e2bb74584877b8ad0eb529320b976264ec2db87420bae242715795f372dbc513ea80047bc49077a064e78205cd5e8b33d746fd2a2912  icu4c-71_1-src.tgz
05eb134a963a541a280e49e4d0aca07e480fef14daa0108c8fb9add18c150c9d34c8cbc46386c07909d511f7777eb3ea9f494001f191b84a7de0be8047da8b56  icu4c-71_1-data.zip
2bee9b93699c40a53c2e768d4678309fbd49e052e197a80fb76842b4001ee65e548459654ef7e7d154b6289f915fccbcb5b3109edcd61e74f0db289f2bec61fd  icu4c-71_1-data-bin-b.zip
07a299b1cd37cd7b07c5e1a7c6871c0dbbeabb2dec6e11c89237f41e56fd29e1a65725be102b4e4ab722183c02810a5df3ed47c3b9b4a7c14bc4c72ab127a27e  icu4c-71_1-data-bin-l.zip
b031e520d41cc313012a0a9d9c4eed51aee9e04213b810bcec32e18d0964f4f26448b989879a9d8901d29024da08ce2ac89c8c6d321c85d78f6414b5edebc1a4  001-fix-heap-buffer-overflow.patch
de2cd008406d133cc838388f5a109560d29323e0a4c8c6306f712a536b6d90846d44bc5f691514621653f33a2929c0d84fa9c54d61d5ddf4606243df63c7e139  skip-flawed-tests.patch
609541c1ac10c12b2b52f7800a2057d5c97e49dc9a1774a1b53e13d88599128baa7637f1a63b4de52dfe58b1038c7f3462ef29ad223dbe2ecb2862e3249a1cf4  data-filter-en.yml
"
